package com.weiyi.mashangzhu.Roomer.Activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Adapter.AwardAdapter;
import com.weiyi.mashangzhu.Roomer.Bean.AwardBean;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AwardActivity extends BaseActivity {

    @BindView(R.id.refresh_list)
    PullToRefreshListView refreshList;
    List<String> list = new ArrayList<>();
    List<AwardBean.DataBean.OrderlistBean> orderlistBeanList = new ArrayList<>();
    AwardAdapter awardAdapter = null;
    int page = 1;
    @BindView(R.id.total)
    TextView total;
    @BindView(R.id.tx)
    TextView tx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_award);
        ButterKnife.bind(this);
        initData(1);
    }

    public void initData(final int pp) {
        super.initView();
        OkHttpUtils.get()
                .addParams("token", SPUtils.get(this, "token", "") + "")
                .addParams("pagenum", pp + "")
                .url(API.BaseApi_L + API.PlatReward)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        T.showShort(AwardActivity.this, "网络连接失败");
                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if (jsonObject.getInt("status") == 0) {
                                total.setText(jsonObject.getJSONObject("data").getDouble("accountall")+"");
                                tx.setText(jsonObject.getJSONObject("data").getDouble("uBalance")+"");
                                if (pp == 1) {
                                    orderlistBeanList.clear();
                                    orderlistBeanList.addAll(new Gson().fromJson(response, AwardBean.class).getData().getOrderlist());

                                    awardAdapter = new AwardAdapter(orderlistBeanList, AwardActivity.this);
                                    refreshList.setAdapter(awardAdapter);
                                } else {
                                    orderlistBeanList.addAll(new Gson().fromJson(response, AwardBean.class).getData().getOrderlist());
                                    awardAdapter.notifyDataSetChanged();
                                }

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
        refreshList
                .setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
                    @Override
                    public void onPullDownToRefresh(
                            PullToRefreshBase<ListView> refreshView) {
                        Log.e("TAG", "onPullDownToRefresh");
                        //这里写下拉刷新的任务
                        new DownGetDataTask().execute();
                    }

                    @Override
                    public void onPullUpToRefresh(
                            PullToRefreshBase<ListView> refreshView) {
                        Log.e("TAG", "onPullUpToRefresh");
                        new UpGetDataTask().execute();
                    }
                });
    }

    private class DownGetDataTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            try {
                Thread.sleep(1000);


            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            page = 1;
            initData(page);
            refreshList.onRefreshComplete();
        }
    }

    private class UpGetDataTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            try {
                Thread.sleep(1000);

            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            page += 1;
            initData(page);
            refreshList.onRefreshComplete();
        }
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
